package com.xl.rentkeeper.act;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.umeng.analytics.MobclickAgent;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.business.LocalCmdConst;
import com.xl.rentkeeper.log.QLog;
import com.xl.rentkeeper.mgr.IUiObserver;
import com.xl.rentkeeper.mgr.UiObserverManager;
import com.xl.fuse.protos.CmdConst;
import com.xl.rentkeeper.util.SystemStatusManager;
import com.xl.rentkeeper.view.TitleBar;

/**
 * Created by zwc on 2015/6/25.
 */
public class BaseAct extends FragmentActivity implements IUiObserver, TitleBar.IconOnClickListener,CmdConst,LocalCmdConst{
    public TitleBar mTBTitle;
    @Override
    public void onEvent(String event, boolean ret, String errInfom,Object[] data) {
        if (CmdLogout.equals(event)) {
            finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        QLog.d(this, "onCreate");
        super.onCreate(savedInstanceState);
        UiObserverManager.getInstance().registerEvent(monitorEvents(), this);
        UiObserverManager.getInstance().registerEvent(CmdLogout,this);
    }
    public void setTranslucentStatus()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            if(mTBTitle!=null){
                mTBTitle.setFull(true);
            }
            Window win = getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            winParams.flags |= bits;
            win.setAttributes(winParams);
        }
        SystemStatusManager tintManager = new SystemStatusManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(0);//状态栏无背景
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
    @Override
    protected void onDestroy() {
        QLog.d(this, "onDestroy");
        super.onDestroy();
        UiObserverManager.getInstance().unregisterEvent(monitorEvents(), this);
    }
    public boolean onBack(int keyCode, KeyEvent event){
        finish();
        overridePendingTransition(R.anim.base_slide_remain, R.anim.base_slide_right_out);
        return false;
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            onBack(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);
    }

    public String[] monitorEvents() {
        return new String[]{CMD_USER_LOGIN};
    }
    public void showSoftInput(EditText et){
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(et, InputMethodManager.SHOW_FORCED);
    }
    public void hideSoftInput(EditText et){
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
    }
    /*public void initToolBar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        //toolbar.setLogo(R.mipmap.ic_launcher);
        toolbar.setTitle("");
        // Toolbar.LayoutParams params = new Toolbar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
        //toolbar.setSubtitle("Sub title");

        setSupportActionBar(toolbar);


        // Navigation Icon 要設定在 setSupoortActionBar 才有作用
        // 否則會出現 back button
        toolbar.setNavigationIcon(R.mipmap.back);
        //给返回按钮设置监听事件
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }*/

    public void initTitleBar(TitleBar view){
        view.setFull(true);
        view.setIconOnClickListener(this);
    }

    @Override
    public void icon1Click()
    {

    }

    @Override
    public void backClick()
    {
        finish();
        overridePendingTransition(R.anim.base_slide_remain, R.anim.base_slide_right_out);
    }
    @Override
    public void icon2Click()
    {
    }

    public void closeKeyboard(View v){
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
}
